﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CompilationFactures.Enums;
using System.IO;

namespace CompilationFactures.Objects
{
    public class FactureEpicerie : Facture
    {
        public FactureEpicerie()
        {

        }

        public FactureEpicerie(int _NoFacture, List<Article> _Articles)
            :base(_NoFacture, _Articles)
        {
            
        }
        
        private FactureEpicerie(FactureEpicerie factEpice)
        {
            this.noFacture = factEpice.noFacture;
            this.operation = factEpice.operation;
            this.listeArticles = factEpice.copierArticles();
        }

        public override string DescriptionType
        {
            get
            {
                return "Facture d'épicerie";
            }
        }

        public override string Type
        {
            get
            {
                return "Épicerie";
            }
        }

        public override Facture Copier()
        {
            return new FactureEpicerie(this);
        }

        static public FactureEpicerie operator +(FactureEpicerie fact1, FactureEpicerie fact2)
        {
            //TODO Compare article avec le nom et le prix
            FactureEpicerie factTotal = new FactureEpicerie(fact1);

            foreach (Article article in fact2.ListeArticles)
            {
                Article ArticleTotal = factTotal.ListeArticles.Find(art => String.Equals(art.Item, article.Item) && art.Prix.Equals(article.Prix));

                if (ArticleTotal != null)
                {
                    ArticleTotal.Quantite = article.Quantite + ArticleTotal.Quantite;
                }
                else
                {
                    factTotal.ListeArticles.Add(article);
                }
            }

            return factTotal;
        }

        static public FactureEpicerie operator -(FactureEpicerie fact1, FactureEpicerie fact2)
        {
            //TODO Compare article avec le nom et le prix
            
            FactureEpicerie factTotal = new FactureEpicerie(fact1);

            foreach (Article article in fact2.ListeArticles)
            {
                Article ArticleTotal = factTotal.ListeArticles.Find(art => String.Equals(art.Item, article.Item) && art.Prix.Equals(article.Prix));

                if (ArticleTotal != null)
                {
                    ArticleTotal.Quantite = ArticleTotal.Quantite - article.Quantite;
                }
            }

            return factTotal;
        }
    }
}
